package com.lf.freezingpoint.common.entity;

import java.io.Serializable;

/*
 * 结果实体
 *
 * */
public class R<T> {

    /**
     * 成功
     */
    public static final int SUCCESS = 200;

    /**
     * 失败
     */
    public static final int FAIL = 500;

    /**
     * 状态码
     */
    private int code;

    /**
     * 消息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    public static <T> R<T> ok() {
        return resetResult(null, SUCCESS, null);
    }

    public static <T> R<T> ok(String msg) {
        return resetResult(null, SUCCESS, msg);
    }

    public static <T> R<T> ok(T data) {
        return resetResult(data, SUCCESS, null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return resetResult(data, SUCCESS, msg);
    }

    public static <T> R<T> fail() {
        return resetResult(null, FAIL, null);
    }

    public static <T> R<T> fail(String msg) {
        return resetResult(null, FAIL, msg);
    }

    public static <T> R<T> fail(T data) {
        return resetResult(data, FAIL, null);
    }

    public static <T> R<T> fail(T data, String msg) {
        return resetResult(data, FAIL, msg);
    }

    public static <T> R<T> fail(int code, String msg) {
        return resetResult(null, code, msg);
    }

    private static <T> R<T> resetResult(T data, int code, String msg) {
        R<T> apiR = new R<>();
        apiR.setCode(code);
        apiR.setData(data);
        apiR.setMsg(msg);
        return apiR;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Boolean isError(R<T> r) {
        return !isSuccess(r);
    }

    public static <T> Boolean isSuccess(R<T> r) {
        return R.SUCCESS == r.getCode();
    }
}
